package cn.itcast.hotel;

import cn.itcast.hotel.pojo.HotelDoc;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.List;

@SpringBootTest
public class HotelSearchDemo {

    private RestHighLevelClient client;

    /**
     *
     * @throws IOException
     */
    @Test
    public void matchAll() throws IOException {
        //创建搜索请求
        SearchRequest request = new SearchRequest("hotel");
        SearchSourceBuilder source = request.source();
        //基础条件
        source.query(QueryBuilders.matchAllQuery());
        //执行搜索请求
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //解析相应结果
        SearchHits hits = response.getHits();
        System.out.println("满足条件结果数量"+hits.getTotalHits().value);
        SearchHit[] docArr = hits.getHits();
        for (SearchHit doc : docArr) {
            String hotelJson = doc.getSourceAsString();
            HotelDoc hotelDoc = JSON.parseObject(hotelJson, HotelDoc.class);
            System.out.println(hotelJson);
        }
    }

    /**
     *
     */
    @Test
    public void aggregations() throws IOException {
        //创建请求
        SearchRequest request = new SearchRequest("hotel");
        request.source().aggregation(
                AggregationBuilders.terms("termsByBrand")
                .field("brand")
                .size(30)
        );
        request.source().size(0);
        //执行请求
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //解析结果
        //聚合结果
        Aggregations aggregations = response.getAggregations();
        //根据自定义的名称 找到指定的聚合结果
        Terms terms= aggregations.get("termsByBrand");
        //获取bucker 分组信息
        List<? extends Terms.Bucket> buckets = terms.getBuckets();
        //遍历分桶集合
        for (Terms.Bucket bucket : buckets) {
            System.out.println("=========================");
            System.out.println("当前分组的key ："+bucket.getKeyAsString());
            System.out.println("当前分组文档的数量 ："+bucket.getDocCount());
            System.out.println("=========================");
        }
    }



    @BeforeEach
    void setUp() {
        this.client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://192.168.200.88:9200")
        ));
    }

    @AfterEach
    void tearDown() throws IOException {
        this.client.close();
    }
}